package com.ztesoft.ossup.report.web;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.ztesoft.ossup.report.utils.Constants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.util.List;
import java.util.Locale;
import java.util.Map;

/**
 * Created by luxiang on 15/10/11.
 */
@Component
public class BaseController implements Constants {
    Logger _logger = LoggerFactory.getLogger(BaseController.class);


    protected static MessageSource messageSource;

    @Autowired
    public void setMessageSource(MessageSource messageSource) {
        BaseController.messageSource = messageSource;
    }

    public String getI18NMessage(String code){
    	Locale locale = new Locale("zh", "CN"); 
    	return messageSource.getMessage(code, null, locale);
    }
    
    public static class PageData<T extends Serializable> {
        long total;
        List<T> rows;

        public PageData(long total, List<T> rows) {
            this.total = total;
            this.rows = rows;
        }

        public long getTotal() {
            return total;
        }

        public void setTotal(long total) {
            this.total = total;
        }

        public List<T> getRows() {
            return rows;
        }

        public void setRows(List<T> rows) {
            this.rows = rows;
        }
    }

    public static class ResponseEntry implements Serializable {
        String code;
        String message;
        Map<String, Object> detail;
        String resolve;

        public ResponseEntry(String code) {
            this.code = code;
        }

        public ResponseEntry(String code, Map<String, Object> detail) {
            this.code = code;
            this.detail = detail;
        }

        public ResponseEntry(String code, String message, Map<String, Object> detail) {
            this.code = code;
            this.message = message;
            this.detail = detail;
        }

        public String getCode() {
            return code;
        }

        public void setCode(String code) {
            this.code = code;
        }

        public String getMessage() {
            if (messageSource != null)
                return messageSource.getMessage(code, null, Locale.getDefault());
            return this.message;
        }

        public void setMessage(String message) {
            this.message = message;
        }

        public Map<String, Object> getDetail() {
            return detail;
        }

        public void setDetail(Map<String, Object> detail) {
            this.detail = detail;
        }

        public String getResolve() {
            return resolve;
        }

        public void setResolve(String resolve) {
            this.resolve = resolve;
        }

        public String getJSON() {
        	Gson gson=new Gson();
            return gson.toJson(this);
        }
    }
}
